/* eslint-disable no-multi-spaces */
const Base = require('./base.js');

module.exports = class extends Base {
  /**
   * 获取支付的请求参数
   * @returns {Promise<PreventPromise|void|Promise>}
   */
  async prepayAction() {
    const orderId = this.get('orderId');

    const orderInfo = await this.model('order')
      .where({ id: orderId })
      .find();
    if (think.isEmpty(orderInfo)) {
      return this.fail(400, '订单已取消');
    }
    if (parseInt(orderInfo.pay_status) !== 0) {
      return this.fail(400, '订单已支付，请不要重复操作');
    }
    const openid = await this.model('user')
      .where({ id: orderInfo.user_id })
      .getField('weixin_openid', true);
    if (think.isEmpty(openid)) {
      return this.fail('微信支付失败');
    }
    const WeixinSerivce = this.service('weixin', 'api');
    try {
      const returnParams = await WeixinSerivce.createUnifiedOrder({
        openid: openid,
        body: '订单编号：' + orderInfo.order_sn,
        out_trade_no: orderInfo.order_sn,
        total_fee: parseInt(orderInfo.actual_price * 100),
        spbill_create_ip: ''
      });
      return this.success(returnParams);
    } catch (err) {
      return this.fail('微信支付失败');
    }
  }

  async notifyAction() {
    const WeixinSerivce = this.service('weixin', 'api');
    const result = WeixinSerivce.payNotify(this.post('xml'));
    if (!result) {
      return `<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[支付失败]]></return_msg></xml>`;
    }

    const orderModel = this.model('order');
    const orderInfo = await orderModel.getOrderByOrderSn(result.out_trade_no);
    if (think.isEmpty(orderInfo)) {
      return `<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[订单不存在]]></return_msg></xml>`;
    }

    if (orderModel.updatePayStatus(orderInfo.id, 2)) {
    } else {
      return `<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[订单不存在]]></return_msg></xml>`;
    }

    return `<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>`;
  }
  async paySuccessAction() {
    const orderId = this.post('orderid');
    const credit = this.post('credit') || 0;
    const balance = this.post('balance') || 0;
    const paytype = this.post('paytype') || 1;
    const cost = this.post('cost') || 0;
    console.log(this.post('cost'));
    const orderInfo = await this.model('order')
      .where({ id: orderId, order_status: ['!=', 9] })
      .find();
    if (think.isEmpty(orderInfo)) {
      return this.fail(400, '订单已取消');
    }
    if (parseInt(orderInfo.pay_status) !== 0) {
      await this.model('order')
        .where({ id: orderId })
        .update({
          order_status: 1
        });
      return this.fail(400, '订单已支付，请不要重复操作');
    }
    const paystatus = await this.model('user')
      .where({
        id: this.getLoginUserId(),
        credit: ['>=', credit],
        balance: ['>=', balance]
      })
      .decrement({ credit: credit, balance: balance });
    if (paystatus == 0) {
      return this.fail(400, '支付失败');
    }
    const result = {};
    result.orderupdate = await this.model('order')
      .where({ id: orderId })
      .update({
        order_status: 1,
        pay_status: 1,
        pay_name: paytype,
        pay_id: 'dhf' + this.model('order').generateOrderNumber(),
        pay_time: this.getTime()
      });

    if (paytype == 1) {
      result.balancesupdate = await this.model('user').distribution(orderId);
      // if (result.balances < 1) {
      //   return this.fail(400, "分销失败");
      // }
      result.creditupdate = await this.model('user').creditupdate(
        this.getLoginUserId(),
        cost
      );
      // if (result.creditupdate < 1) {
      //   return this.fail(400, "积分失败");
      // }
    }

    return this.success(result);
  }
};
